using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;

namespace Moogle
{
    /// <summary>
    /// Fill/Remind Activity Class.  This activity is used to modify the user existing fill reminders.
    /// It's target consumer is the ActivityRemind class.
    /// </summary>
    [Activity(Label = "Fill Reminders", MainLauncher = false, Icon = "@drawable/icon", Theme = "@android:style/Theme.Holo.Light.Dialog", NoHistory = true)]
    [IntentFilter(new[] { Intent.ActionMain })]
    public class ActivityFillRemind : Activity
    {
        #region Local Memory
        /// <summary>
        /// Constant using to identify (filter) entries created in the
        /// Android Log by this class.
        /// </summary>
        private const string C_TAG = "ActivityFillRemind";
        private const int __C_DATE_DIALOG_ID = 0;
        private TextView __M_textViewDateDisplay;
        private Button __M_buttonPickDate;
        private DateTime __M_dateTimeSelected;
        #endregion
        #region OnCreate Control Function
        /// <summary>
        /// Class (or Activity) entry point.
        /// </summary>
        /// <param name="bundle">Android.OS.Bundle object instance that can be used to pass data to activity at startup.</param>
        protected override void OnCreate(Bundle bundle)
        {

            base.OnCreate(bundle);

            Log.Info(C_TAG, "OnCreate started.");

            try
            {
                /// Load the activities layout 
                this.SetContentView(Resource.Layout.remindfill);

                /// capture our View elements
                this.__M_textViewDateDisplay = (TextView)this.FindViewById(Resource.Id.dateDisplay);
                this.__M_buttonPickDate = (Button)this.FindViewById(Resource.Id.pickDate);
                /// add a click listener to the Pick Date button        
                this.__M_buttonPickDate.Click += delegate
                {
                    this.ShowDialog(__C_DATE_DIALOG_ID);
                }; // end delegate

                /// get the current date        
                this.__M_dateTimeSelected = DateTime.Today;
                
                /// Initiate the Update process
                UpdateDisplay();
            } // end try
            catch (SystemException err)
            {
                Log.Error(C_TAG, err.Message);
            } // end catch
            finally
            {
                Log.Info(C_TAG, "OnCreate ended.");
            } // end finally
        }
        #endregion
        #region Date Input Processing Control Functions
        /// <summary>
        /// Control function used to update the date in the 
        /// TextView
        /// </summary>
        private void UpdateDisplay()
        {
            Log.Info(C_TAG, "UpdateDisplay started.");
            try
            {
                this.__M_textViewDateDisplay.Text = this.__M_dateTimeSelected.ToString("d");
            } // end try
            catch (SystemException err)
            {
                Log.Error(C_TAG, err.Message);
            } // end catch
            finally
            {
                Log.Info(C_TAG, "UpdateDisplay ended.");
            } // end finally
        }
        /// <summary>
        /// control function used to set the event 
        /// received when the user "sets" the date in the dialog 
        /// </summary>
        /// <param name="sender">calling object instance</param>
        /// <param name="e">DatePickerDialog.DateSetEventArgs object instance</param>
        private void OnDateSet(object sender, DatePickerDialog.DateSetEventArgs e)
        {
            Log.Info(C_TAG, "OnDateSet started.");
            try
            {
                this.__M_dateTimeSelected = e.Date;
                UpdateDisplay();
          
            } // end try
            catch (SystemException err)
            {
                Log.Error(C_TAG, err.Message);
            } // end catch
            finally
            {
                Log.Info(C_TAG, "OnDateSet ended.");
            } // end finally
        }
        #endregion
        #region base class overrides
        protected override Dialog OnCreateDialog(int id)
        {
            switch (id)
            {
                case __C_DATE_DIALOG_ID:
                    return new DatePickerDialog(this, OnDateSet, this.__M_dateTimeSelected.Year, this.__M_dateTimeSelected.Month - 1, this.__M_dateTimeSelected.Day);
            }
            return null;
        }
        #endregion
    }
}